package com.longyu.crowd.mvc.handler;

import com.github.pagehelper.PageInfo;
import com.longyu.crowd.constant.CrowdConstant;
import com.longyu.crowd.entity.Admin;
import com.longyu.crowd.service.api.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
public class AdminHandler {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/admin/update.html")
    public String update(Admin admin,
                         @RequestParam("pageNum") int pageNum,
                         @RequestParam("keyword") String keyword) {


        adminService.update(admin);

        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }


    /**
     * 执行更新的方法
     *
     * @param adminId
     * @param modelMap
     * @return
     */
    @RequestMapping("/admin/to/edit/page.html")
    public String toEditPage(
            @RequestParam("adminId") Integer adminId,
            ModelMap modelMap
    ) {

        // 1.根据adminId查询Admin对象
        Admin admin = adminService.getAdminById(adminId);

        // 2.将Admin对象存入模型
        modelMap.addAttribute("admin", admin);
        return "admin-edit";
    }


    @RequestMapping("/admin/save.html")
    public String save(Admin admin) {

        adminService.saveAdmin(admin);

        return "redirect:/admin/get/page.html?pageNum=" + Integer.MAX_VALUE;
    }


    /**
     * 登录Controller
     *
     * @param loginAcct
     * @param userPswd
     * @param session
     * @return
     */
    @RequestMapping("admin/do/login.html")
    public String doLogin(@RequestParam String loginAcct, @RequestParam String userPswd, HttpSession session) {

        // 调用Service方法 执行登录检查
        // 如果这个方法能够返回admin对象说明登录成功，如果账号、密码不正确则会抛出异常
        Admin admin = adminService.getAdminByLoginAcct(loginAcct, userPswd);

        // 将登录成功返回的admin对象存入Session域
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, admin);

        return "redirect:/admin/to/main/page.html";
    }


    /**
     * 退出登录控制器
     *
     * @param session
     * @return
     */
    @RequestMapping("/admin/do/logout.html")
    public String toLogout(HttpSession session) {

        // 强制Session失效
        session.invalidate();

        return "redirect:/admin/do/login/page.html";
    }

    @RequestMapping("/admin/get/page.html")
    public String getPageInfo(

            // 使用@RequestParam注解的defaultValue属性，指定默认值，在请求中没有携带时使用默认值
            // keyword默认值使用空字符串，和SQL语句配合实现两种情况适配
            @RequestParam(value = "keyword", defaultValue = "") String keyword,

            // pageNum默认值使用1
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,

            // pageSize默认值使用5
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            ModelMap modelMap

    ) {

        // 调用Service方法获取PageInfo对象
        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);

        // 将pageInfo对象存入模型
        modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);

        return "admin-page";
    }


    @RequestMapping("/admin/remove/{adminId}/{pageNum}/{keyword}.html")
    public String remove(@PathVariable("adminId") Integer adminId, @PathVariable("pageNum") Integer pageNum, @PathVariable("keyword") String keyword, HttpSession session, RedirectAttributes redirectAttributes) {


        Admin admin = (Admin) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN);
        if (admin.getId() == adminId) {
            redirectAttributes.addAttribute(CrowdConstant.ATTR_NAME_EXCEPTION, "no remove me！");
        } else {
            // 执行删除
            adminService.remove(adminId);

            // 1、直接转发到admin-page.jsp会无法显示分页数据
//        return "admin-page";
            // 2、转发到/admin/get/page.html地址,一旦刷新页面会重新执行删除浪费性能
//        return "forward:/admin/get/page.html";


            // 3、重定向到/admin/get/page.html地址
            // 同时为了保持原本所在的页面和查询关键词再附加pageNum和keyword两个请求参数
        }
        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }


}
